#include "Buoyancy.h"

namespace poly
{
	Buoyancy::Buoyancy()
	{
		forceDensity = zero3();
	}

	void Buoyancy::apply(float dt)
	{
		for( RigidPolySet::iterator rp_itr = bodies.begin();
			rp_itr != bodies.end(); rp_itr++ )
		{
			RigidPoly & rp = **rp_itr;
			Polygon transformedPoly = rp.polygon().transform( rp.rigidBody.world() );
			for( PolygonSet::iterator poly_itr = buoyant.begin();
				poly_itr != buoyant.end(); poly_itr++ )
			{
				Polygon & poly = **poly_itr;

				Polygon conj = conjunction( transformedPoly, poly );
				if( conj.size() > 0 )
				{
					float3 ptOfApp = conj.center();
					float3 force = conj.area() * forceDensity;

					rp.rigidBody.addForce( force, ptOfApp );
				}
			}
		}
	}
}
